package com.xintonghua.bussiness.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.base.SimpleBaseAdapter;
import com.xintonghua.bussiness.bean.ServiceItems;
import com.xintonghua.bussiness.util.MyUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者：wanxianyi
 * 时间：2018/3/11.
 * 包名：com.xintonghua.bussiness.adapter
 */

public class GoodsAdapter extends SimpleBaseAdapter<ServiceItems> {


    public GoodsAdapter(List<ServiceItems> list, Context context) {
        super(list, context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        GoodsViewHolder viewHolder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_goods_adapter, null);
            viewHolder = new GoodsViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (GoodsViewHolder) convertView.getTag();
        }
        ServiceItems items = mList.get(position);

        viewHolder.tvServiceName.setText(items.getUserName());
        viewHolder.tvServicePrice.setText(MyUtils.getMoney(context, items.getMoney()));
        viewHolder.tvPersonalName.setText(items.getPersonalName());
        addItem(viewHolder.llGoods, items);
        viewHolder.tvMemberCardname.setText(TextUtils.isEmpty(items.getMemberCardName())?"":(items.getMemberCardName()+"-1"));

        return convertView;
    }

    private void addItem(LinearLayout ll, ServiceItems items) {
        ll.removeAllViews();
        for (ServiceItems.StockProductsBean bean : items.getStockProducts()) {
            View view = mInflater.inflate(R.layout.product_item, ll, false);
            TextView tv_product_name = view.findViewById(R.id.tv_product_name);
            TextView tv_product_price = view.findViewById(R.id.tv_product_price);
            TextView tv_product_type = view.findViewById(R.id.tv_product_type);

            tv_product_name.setText(bean.getName() + "x" + bean.getAmount());
            tv_product_price.setText(MyUtils.getMoney(context, bean.getPrice()));
            tv_product_type.setText(items.getPositionName());
            ll.addView(view);
        }

    }

    class GoodsViewHolder {
        @BindView(R.id.tv_service_name)
        TextView tvServiceName;
        @BindView(R.id.tv_service_price)
        TextView tvServicePrice;
        @BindView(R.id.tv_personalName)
        TextView tvPersonalName;
        @BindView(R.id.ll_goods)
        LinearLayout llGoods;
        @BindView(R.id.tv_member_cardname)
        TextView tvMemberCardname;

        public GoodsViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
